package org.ISCS.service.utilimpl;

import org.ISCS.entity.Dependency;
import org.springframework.stereotype.Service;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

@Service
public class ReadDependencyService {

    public String readDependencyData(Dependency dependency, String filePath) {
        Resource resource = new FileSystemResource(filePath);
        try (InputStream inputStream = resource.getInputStream();
             Scanner scanner = new Scanner(inputStream)) {
            StringBuilder fileContent = new StringBuilder();
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                fileContent.append(line).append("\n");
            }
            // 转为String并去掉最后一个换行符
            String dependencyData = fileContent.toString().substring(0, fileContent.length() - 1);
            // 通过dependency.setDependencyData()方法存入
            return  dependencyData;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "null";
    }
}
